clear all
set more off

// ***************** LOAD DATA ******************** //

// Contracts
use "contract_data_var.dta", clear 

// Drop sectors we don't have complete contracts data. 
drop if inlist(naics2, "55", "99")
reshape wide raw_obligations, i(year month) j(naics2, string)

tempfile contracts
save `contracts'

// Shadow Rates
import excel using "shadowrate_monthly.xls", firstrow clear 
keep month year shadow_rate
destring month, replace
destring year, replace 
tempfile shadow
save `shadow' 

import excel using "mgdp.xlsx", firstrow clear
keep month year nominal_gdp real_gdp 
destring year, replace 
drop if nominal_gdp == . 
tempfile mgdp 
save `mgdp' 


// Macro data from FRED
// User must have own API key 
clear 
set fredkey "XXXXXXXXXXXXXXXXXXXXXX" //INSERT API KEY HERE 

clear 
import fred "DGS2"  
replace daten = mdy(month(daten),1,year(daten))
collapse (mean) DGS2, by(daten) 
tempfile dgs2
save `dgs2' 

clear 
import fred "INDPRO" "BBKMGDP" "UNRATE" "CPIAUCSL" "CPILFESL" "PCEPI" "PCEPILFE" "PPIACO" "IPMAN" "USALORSGPNOSTSAM" "W055RC1" "MICH" "PCTR" "MCOILWTICO" "EXPINF1YR"

gen year  = year(daten) 
gen month = month(daten) 
order year month, first

rename MCOILWTICO wti 

merge 1:1 year month using `contracts', nogen 


// Merge in shadow rate 
merge 1:1 year month using `shadow', nogen 
merge 1:1 year month using `mgdp', nogen 
merge 1:1 daten using `dgs2', nogen 

keep if inrange(year, 1999, 2019) 


// ************* Choose Sector Groups ************ //
gen sticky_raw    = 0 
gen flex_raw      = 0 


// Sticky Sectors
local slist 32 56 54 33 
 
// Flex Sectors 
local flist  11 21 22 23 31 42 44 45 48 49 51 52 61 62 72 71 81

foreach sec in `slist' {
	replace sticky_raw    = sticky_raw    + raw_obligations`sec'
}


foreach sec in `flist' {
	replace flex_raw    = flex_raw    + raw_obligations`sec'
}


gen agg_raw = sticky_raw + flex_raw 

// ******** Prep Data for VAR **************// 
gen datem = mofd(daten)
drop daten 
tsset datem, monthly 

// Month dummies for VAR 
foreach mm of numlist 1/12 {
	gen month_`mm' = month == `mm' 
}

gen q1 = month_1 + month_2 + month_3 == 1
gen q2 = month_4 + month_5 + month_6 == 1
gen q3 = month_7 + month_8 + month_9 == 1
gen q4 = month_10 + month_11 + month_12 == 1


// Monthly gdp index, cumulative sum 
gen gdp_monthly = BBKMGDP 
sort datem 
replace gdp_monthly = BBKMGDP + L.gdp_monthly if _n > 1

// Take Logs 
gen gdp_deflator = 100*(nominal_gdp/real_gdp_index) 

local inflatvar PCEPI  

// Deflate contracts
gen sticky_raw_real = sticky_raw/`inflatvar'
gen flex_raw_real   = flex_raw/`inflatvar'

gen lgdp    = log(real_gdp_index) 
gen linflat = log(`inflatvar') - log(L12.`inflatvar')
gen intrate  = DGS2/100
gen shadow  = shadow/100 

gen lsticky = log(sticky_raw_real)
gen lflex   = log(flex_raw_real) 

gen lwti    = log(wti) 


// ******** VAR ***************// 
var linflat lwti lgdp intrate lflex lsticky if year > 2001, lags(1/12) exog(datem month_*)   
irf create irf1,  order(linflat lwti lgdp intrate lflex lsticky) set(vecirfs) step(24) replace

irf graph oirf, irf(irf1) impulse(lflex) response(lflex)
graph save flex_shock, replace 

irf graph oirf, irf(irf1) impulse(lflex) response(lgdp) 
graph save flex_gdp, replace

irf graph oirf, irf(irf1) impulse(lflex) response(linflat)
graph save flex_inflat, replace 

irf graph oirf, irf(irf1) impulse(lflex) response(intrate) 
graph save flex_shadow, replace 

irf graph oirf, irf(irf1) impulse(lsticky) response(lsticky)
graph save sticky_shock, replace

irf graph oirf, irf(irf1) impulse(lsticky) response(lgdp)
graph save sticky_gdp, replace

irf graph oirf, irf(irf1) impulse(lsticky) response(linflat)
graph save sticky_inflat, replace

irf graph oirf, irf(irf1) impulse(lsticky) response(intrate) 
graph save sticky_shadow, replace

graph combine "flex_shock" "flex_gdp" "flex_inflat" "flex_shadow" "sticky_shock" "sticky_gdp" "sticky_inflat" "sticky_shadow", rows(2) cols(4) 


